<?php

class Mail
{
     public $messages;

     public function __construct()
     {
         $data=SQL::select(Template::render("getMessages.sql", "sql", array
		 (
			 'sender_id'=>$_SESSION['user_id'],
			 'receiver_id'=>$_SESSION['user_id'],
			 'my_id'=>$_SESSION['user_id']
			 
		 )));
         
         if($data)
         {
             $this->messages=$data;            
         }
		 else 
		 {
			 $this->messages=0; 
		 }
     }
     
     public function getMessages()
	 {
		 return $this->messages;
	 }
     
     public static function getMessageConversation($receiver, $sender)
	 {
		 $data=SQL::select(Template::render("getMessageConversation.sql", "sql", array
		  (
			 'sender_id'=>$sender,
			 'receiver_id'=>$receiver,
			 'my_id'=>$_SESSION['user_id']
			 
		 )));
		 
		 if ($data)
		 {
			 return $data;			 			 
		 }
		 else
		 {
			 return false;
		 }
	 }
	 
	 public static function updateMessageReadStatus($receiver, $sender)
	 {
		 SQL::execute(Template::render("updateMessageReadStatus.sql", "sql", array('sender_id'=>$sender, 'receiver_id'=>$receiver)));
	 }
	 
	  public static function sendMessage($receiver, $message)
	  {
		 return SQL::execute(Template::render("sendMessage.sql", "sql", array
		 (
			 'sender_id'=>$_SESSION['user_id'],
			 'receiver_id'=>$receiver,
			 'date'=>time(),
			 'body'=>$message,
			 'status'=>'unread'			 
		 )));
	  }
	  
	  public static function countUnreadMessages()
	  {
		 return SQL::num_rows(Template::render("countUnreadMessages.sql", "sql", array('receiver_id'=>$_SESSION['user_id'])));
	  }
}